<?php

namespace app\common\library;

use CURLFile;

trait Broadcast
{
    //获取微信token
    public function getAccessToken()
    {
        $appId = get_sys_config('app_id');

        $secret = get_sys_config('secret');
        $url = sprintf(get_sys_config('get_access_token_url'), $appId, $secret);
        $res = curl_get($url);
        $arr = json_decode($res, true);
        return $arr['access_token'];
    }

    //查看人员列表
    public function wxRoleList()
    {
        $token = $this->getAccessToken();
        $url = sprintf(get_sys_config('get_role_list'), $token, 1);
        $res = curl_get($url);
        $arr = json_decode($res, true);
        return $arr;
    }

    //添加人员权限
    public function addWxRole($data)
    {
        $token = $this->getAccessToken();
        $url = sprintf(get_sys_config('add_wx_role'), $token);
        $headers = [
            'Content-Type: application/json',
        ];
        $res = curl_post($url, json_encode($data), $headers);
        $arr = json_decode($res, true);
        return $arr;
    }

    //移除成员角色
    public function deleteWxRole($data)
    {
        $token = $this->getAccessToken();
        $url = sprintf(get_sys_config('delete_role'), $token);
        $headers = [
            'Content-Type: application/json',
        ];
        $res = curl_post($url, json_encode($data), $headers);
        $arr = json_decode($res, true);
        return $arr;
    }

    //获取直播间
    public function getLiveInfoList($data)
    {
        $token = $this->getAccessToken();
        $url = sprintf(get_sys_config('get_live_info'), $token);
    
        $headers = [
            'Content-Type: application/json',
        ];
        $res = curl_post($url, json_encode($data), $headers);
        $arr = json_decode($res, true);
        return $arr;
    }

    //创建直播间
    public function createRoom($data)
    {
        $token = $this->getAccessToken();
        $url = sprintf(get_sys_config('create_room'), $token);
        $headers = [
            'Content-Type: application/json',
        ];
        $res = curl_post($url, json_encode($data), $headers);
        $arr = json_decode($res, true);
        return $arr;
    }

    //上传素材获取mediaId
    public function uploadMediaID($path)
    {
        $file = (realpath(public_path() . $path));
        $token = $this->getAccessToken();
        $url            = sprintf(get_sys_config('get_media_id'), $token);
        $data['media'] = new CURLFile($file);
        $mediaID        = curl_post($url, $data);
        $arr = json_decode($mediaID, true);
        return $arr['media_id'];
    }


    //删除直播间
    public function delRoom($room_id)
    {
        $data['id'] = $room_id;
        $token = $this->getAccessToken();
        $url = sprintf(get_sys_config('delete_room'), $token);
        $headers = [
            'Content-Type: application/json',
        ];
        $res = curl_post($url, json_encode($data), $headers);
        $arr = json_decode($res, true);
        return $arr;
    }

    //编辑直播间
    public function editRoom($data)
    {
        $token = $this->getAccessToken();
        $url = sprintf(get_sys_config('edit_room'), $token);
        $headers = [
            'Content-Type: application/json',
        ];
        $res = curl_post($url, json_encode($data), $headers);
        $arr = json_decode($res, true);
        return $arr;
    }



    //导入商品
    /**
     * {"ids": [1150, 1111], "roomId": 2554}  // 数组列表，可传入多个，里面填写 商品 ID
     */
    public function importGoods($data)
    {
        $token = $this->getAccessToken();
        $url = sprintf(get_sys_config('import_goods'), $token);
        $headers = [
            'Content-Type: application/json',
        ];
        $res = curl_post($url, json_encode($data), $headers);
        $arr = json_decode($res, true);
        return $arr;
    }


    //推送商品
    /**
     * {"roomId": 6474,"goodsId" : 1} 
     */
    public function pushGoods($data)
    {
        $token = $this->getAccessToken();
        $url = sprintf(get_sys_config('good_push'), $token);
        $headers = [
            'Content-Type: application/json',
        ];
        $res = curl_post($url, json_encode($data), $headers);
        $arr = json_decode($res, true);
        return $arr;
    }

    /**
     * 上下架商品
     * {"roomId": 6474,"goodsId" : 1,"onSale": 0} 上下架 【0：下架，1：上架】
     */
    public function saleGoods($data)
    {
        $token = $this->getAccessToken();
        $url = sprintf(get_sys_config('sale_goods'), $token);
        $headers = [
            'Content-Type: application/json',
        ];
        $res = curl_post($url, json_encode($data), $headers);
        $arr = json_decode($res, true);
        return $arr;
    }

    /**
     * 添加并提审商品
     */
    public function addGoods($data)
    {
        $token = $this->getAccessToken();
        $url = sprintf(get_sys_config('add_goods'), $token);
        $headers = [
            'Content-Type: application/json',
        ];

        $res = curl_post($url, json_encode($data), $headers);
        $arr = json_decode($res, true);
        return $arr;
    }

    //重新提交商品审核
    public function resubmitAudit($data)
    {
        $token = $this->getAccessToken();
        $url = sprintf(get_sys_config('resubmit_audit'), $token);
        $headers = [
            'Content-Type: application/json',
        ];
        $res = curl_post($url, json_encode($data), $headers);
        $arr = json_decode($res, true);
        return $arr;
    }

    //获取商品的信息与审核状态
    public function getGoodsAuditInfo($data)
    {
        $token = $this->getAccessToken();
        $url = sprintf(get_sys_config('get_goods_audit_info'), $token);
        $headers = [
            'Content-Type: application/json',
        ];
        $res = curl_post($url, json_encode($data), $headers);
        $arr = json_decode($res, true);
        return $arr;
    }

    //商品撤回审核
    public function resetAudit($data)
    {
        $token = $this->getAccessToken();
        $url = sprintf(get_sys_config('reset_audit'), $token);
        $headers = [
            'Content-Type: application/json',
        ];
        $res = curl_post($url, json_encode($data), $headers);
        $arr = json_decode($res, true);
        return $arr;
    }


    //更新商品
    public function updateGoodsinfo($data)
    {
        $token = $this->getAccessToken();
        $url = sprintf(get_sys_config('update_goods_info'), $token);
        $headers = [
            'Content-Type: application/json',
        ];
        $res = curl_post($url, json_encode($data), $headers);
        $arr = json_decode($res, true);
        return $arr;
    }

    //获取商品列表
    public function getGoodsInfo($data)
    {
        $token = $this->getAccessToken();
        $url = sprintf(get_sys_config('get_goods_info'), $token);
        $headers = [
            'Content-Type: application/json',
        ];
        $res = curl_post($url, json_encode($data), $headers);
        $arr = json_decode($res, true);
        return $arr;
    }

    //删除商品
    public function deleteGoodsInfo($data)
    {
        $token = $this->getAccessToken();
        $url = sprintf(get_sys_config('delete_goods_info'), $token);
        $headers = [
            'Content-Type: application/json',
        ];
        $res = curl_post($url, json_encode($data), $headers);
        $arr = json_decode($res, true);
        return $arr;
    }


    //生成小程序码
    public function getWxCodeUnLimit($data)
    {
        $token = $this->getAccessToken();
        $url = sprintf(get_sys_config('wxacodeunlimit'), $token);
        $headers = [
            'Content-Type: application/json;charset=utf-8',
        ];
        $res = curl_post($url, json_encode($data),$headers);


        return $res;
    }
}
